package com.servlet;

import com.beans.OrderGoods;
import com.beans.OrderInfo;
import com.beans.PageInfo;
import com.common.Enums;
import com.dao.OrderDao;
import com.utils.PageUtil;
import com.utils.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/OrderServlet.do")
public class OrderServlet extends HttpServlet {
    OrderDao orderDao=new OrderDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        if("manage".equals(flag)){
            manage(request,response);
        }
        else if("orderview".equals(flag)){
            orderview(request,response);
        }
        else if("sendgoods".equals(flag)){
            sendgoods(request,response);
        }
        else if("del".equals(flag)){
            del(request,response);
        }
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int orderId=Integer.parseInt(request.getParameter("id"));
        orderDao.deleteOrder(orderId);
        request.setAttribute("msg", "订单删除成功! ");
        manage(request, response);
    }

    private void sendgoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int orderId=Integer.parseInt(request.getParameter("id"));
        if(orderDao.sendGoods(orderId,Enums.EnumOrderState.已发货.toString())==1){
            request.setAttribute("msg","发货操作成功!");
            manage(request,response);
        }
    }

    private void orderview(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int orderId=Integer.parseInt(request.getParameter("id"));
        OrderInfo order=orderDao.getOrderById(orderId);
        List<OrderGoods>goodsList=orderDao.getGoodslist(orderId);

        request.setAttribute("order",order);
        request.setAttribute("goodsList", goodsList);

        request.getRequestDispatcher("/order/order_details.jsp").forward(request, response);

    }

    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderNo= StrUtil.isNullOrEmpty(request.getParameter("orderNo"))?"":request.getParameter("orderNo");
        String beginDate=StrUtil.isNullOrEmpty(request.getParameter("beginDate"))?"":request.getParameter("beginDate");
        String endDate=StrUtil.isNullOrEmpty(request.getParameter("endDate"))?"":request.getParameter("endDate");
        String orderState=StrUtil.isNullOrEmpty(request.getParameter("orderState"))?"":request.getParameter("orderState");

        int pageIndex=StrUtil.isNullOrEmpty(request.getParameter("pageIndex"))?1:Integer.parseInt(request.getParameter("pageIndex"));

        int rowCount=orderDao.getOrderCount(orderNo,orderState,beginDate,endDate);
        PageInfo page= PageUtil.getPageInfo(Enums.PAGESIZE, rowCount, pageIndex);
        List<OrderInfo> orderList=orderDao.getOrderList(orderNo,orderState,beginDate,endDate,page);

        request.setAttribute("orderList", orderList);
        request.setAttribute("pageInfo", page);
        request.getRequestDispatcher("/order/order_manage.jsp").forward(request, response);
    }
}
